﻿using Manage.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Manage.Basic
{
    public partial class FrmAreaMgr : Form
    {
        public FrmAreaMgr()
        {
            InitializeComponent();
        }
        private List<AreaProvinceModel> prList;
        private List<AreaCityModel> ctList;
        private List<AreaDistrictModel> dtList;
        private bool isInit;

        private int Spid;
        private int Scid;
        private int Sdid;
        /// <summary>
        /// 显示界面后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmAreaMgr_Shown(object sender, EventArgs e)
        {
            isInit = true;
            //读取地区列表
            Dictionary<string, string> dcProvince = new Dictionary<string, string>();
            dcProvince.Add("Token", Share.MoM.Token);
            RetModel rm = Share.PostData("51API/Company/GetProvinceList", dcProvince, null);
            if (!rm.Success)
            {
                MessageBox.Show("读取省份列表失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息:" + rm.ErrInfo);
            }
            else
            {
                AreaProvinceModel[] pr = JsonConvert.DeserializeObject<AreaProvinceModel[]>(rm.Data.ToString());
                DataTable dt = new DataTable();
                dt.Columns.Add("id");
                dt.Columns.Add("省名");
                for (int i = 0; i <pr.Length ; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["Id"] = pr[i].Id;
                    dr["省名"] = pr[i].Name;
                    dt.Rows.Add(dr);
                }
                gridControl1.DataSource = dt;
                gridView1.Columns["id"].Visible = false;
                prList = pr.ToList();
            }
            
        }
        /// <summary>
        /// 选中省表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Spid = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gridView1.Columns["id"]));
            if (isInit)
                return;
            try
            {
                isInit = true;
                var obj = Spid;
                if (0 == obj)
                {
                    gridControl2.DataSource = null;
                    isInit = false;
                    return;
                }
                gridControl2.DataSource = null;

                var res = from x in prList where x.Id == Spid select x;
                AreaProvinceModel p = null;
                if (res.Any())
                {
                    p = res.ToList()[0];
                }
                ctList = p.Cities.ToList();
                gridControl2.DataSource = ctList;
                isInit = false;
            }
            catch (Exception)
            {
                isInit = false;
                throw;
            }
        }
        /// <summary>
        /// 选中市表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView2_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                Scid = Convert.ToInt32(gridView2.GetRowCellValue(gridView2.GetSelectedRows()[0], gridView2.Columns["id"]));
                isInit = true;
                var obj = Spid;
                if (0 == obj)
                {
                    gridControl3.DataSource = null;
                    isInit = false;
                    return;
                }
                gridControl3.DataSource = null;

                var res = from x in ctList where x.Id == Scid select x;
                AreaCityModel c = null;
                if (res.Any())
                {
                    c = res.ToList()[0];
                }
                dtList = c.Districts.ToList();
                gridControl3.DataSource = dtList;
                isInit = false;
            }
            catch (Exception)
            {
                isInit = false;
                throw;
            }
        }
        /// <summary>
        /// 选中县表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridView3_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                Sdid = Convert.ToInt32(gridView3.GetRowCellValue(gridView3.GetSelectedRows()[0], gridView3.Columns["id"]));
            }
            catch (Exception)
            {

                throw;
            }
        }
        #region 省
        /// <summary>
        /// 添加省
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProvince_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                AreaProvinceModel apm = new AreaProvinceModel();
                apm.Name = tbxProvince.Text.Trim();
                RetModel rm = Share.PostData("", dc,JsonConvert.SerializeObject(apm));

                if (rm.Success)
                {
                    MessageBox.Show("添加省成功！");
                    return;
                }
                else
                {
                    MessageBox.Show("添加失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 修改省
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                AreaProvinceModel apm = new AreaProvinceModel();
                apm.Id = Spid;
                apm.Name = tbxProvince.Text.Trim();
                RetModel rm = Share.PostData("51API/Company/ProvinceEdit", dc, JsonConvert.SerializeObject(apm));
                if (rm.Success)
                {
                    MessageBox.Show("修改省完成！");

                }
                else
                {
                    MessageBox.Show("修改失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        /// <summary>
        /// 删除省
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                dc.Add("id", Spid.ToString());
                
                RetModel rm = Share.PostData("51API/Company/ProvinceDel", dc, null);
                if (rm.Success)
                {
                    MessageBox.Show("删除省完成！");

                }
                else
                {
                    MessageBox.Show("删除省失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询省
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                RetModel rm = Share.PostData("51API/Company/GetProvinceList", dc, null);
                if (rm.Success)
                {
                    AreaProvinceModel[] apm = JsonConvert.DeserializeObject<AreaProvinceModel[]>(rm.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("省名");
                    for (int i = 0; i < apm.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = apm[i].Id;
                        dr["省名"] = apm[i].Name;
                        dt.Rows.Add(dr);
                    }
                    gridControl1.DataSource = dt;
                    gridView1.Columns["id"].Visible = false;
                }
                else
                {
                    MessageBox.Show("查询省失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        #endregion

        #region 市
        /// <summary>
        /// 添加市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCity_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                AreaCityModel acm = new AreaCityModel();
                acm.Name = tbxCity.Text.Trim();
                RetModel rm = Share.PostData("51API/Company/CityAdd", dc, JsonConvert.SerializeObject(acm));

                if (rm.Success)
                {
                    MessageBox.Show("添加市成功！");
                    return;
                }
                else
                {
                    MessageBox.Show("添加市失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 修改市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton6_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                AreaCityModel acm = new AreaCityModel();
                acm.Id = Scid;
                acm.Name = tbxCity.Text.Trim();
                RetModel rm = Share.PostData("51API/Company/CityEdit", dc, JsonConvert.SerializeObject(acm));
                if (rm.Success)
                {
                    MessageBox.Show("修改市完成!");
                    return;
                }
                else
                {
                    MessageBox.Show("修改市失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                
            }
        }
        /// <summary>
        /// 删除市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton5_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                dc.Add("id", Scid.ToString());
                RetModel rm = Share.PostData("51API/Company/CityDel", dc, null);
                if (rm.Success)
                {
                    MessageBox.Show("删除市完成！");
                    return;
                }
                else
                {
                    MessageBox.Show("s删除市失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 查询市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                dc.Add("Pid", Spid.ToString());
                RetModel rm = Share.PostData("51API/Company/GetCityList", dc, null);
                if (rm.Success)
                {
                    AreaCityModel[] apm = JsonConvert.DeserializeObject<AreaCityModel[]>(rm.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("市名");
                    for (int i = 0; i < apm.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = apm[i].Id;
                        dr["市名"] = apm[i].Name;
                        dt.Rows.Add(dr);
                    }
                    gridControl2.DataSource = dt;
                    gridView2.Columns["id"].Visible = false;
                }
                else
                {
                    MessageBox.Show("查询市失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
        #region 县/区
        /// <summary>
        /// 添加县区
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDistrict_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                AreaDistrictModel adm = new AreaDistrictModel();
                adm.Name = tbxDistrict.Text.Trim();
                RetModel rm = Share.PostData("51API/Company/DistrictAdd", dc, JsonConvert.SerializeObject(adm));

                if (rm.Success)
                {
                    MessageBox.Show("添加县/区成功！");
                    return;
                }
                else
                {
                    MessageBox.Show("添加县失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 修改县
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton8_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                AreaDistrictModel adm = new AreaDistrictModel();
                adm.Id = Sdid;
                adm.Name = tbxDistrict.Text.Trim();
                RetModel rm = Share.PostData("51API/Company/DistrictEdit", dc, JsonConvert.SerializeObject(adm));
                if (rm.Success)
                {
                    MessageBox.Show("修改县完成！");
                    return;
                }
                else
                {
                    MessageBox.Show("修改县失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 删除县
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton9_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                dc.Add("id", Sdid.ToString());
                RetModel rm = Share.PostData("51API/Company/DistrictDel", dc, null);
                if (rm.Success)
                {
                    MessageBox.Show("删除县完成！");
                    return;
                }
                else
                {
                    MessageBox.Show("删除县失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 查询县
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton7_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dc = new Dictionary<string, string>();
                dc.Add("Token", Share.MoM.Token);
                dc.Add("Cid", Scid.ToString());
                RetModel rm = Share.PostData("51API/Company/GetDistrictList", dc, null);
                if (rm.Success)
                {
                    AreaDistrictModel[] adm = JsonConvert.DeserializeObject<AreaDistrictModel[]>(rm.Data.ToString());
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");
                    dt.Columns.Add("县名");
                    for (int i = 0; i < adm.Length; i++)
                    {
                        DataRow dr = dt.NewRow();
                        dr["id"] = adm[i].Id;
                        dr["县名"] = adm[i].Name;
                        dt.Rows.Add(dr);
                    }
                    gridControl3.DataSource = dt;
                    gridView3.Columns["id"].Visible = false;
                }
                else
                {
                    MessageBox.Show("查询县失败！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
        /// <summary>
        /// 载入界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmAreaMgr_Load(object sender, EventArgs e)
        {
            try
            {

            }
            catch (Exception)
            {

                throw;
            }
        }

       
    }
}
